package com.hellobike.haxing.modules.wms.facade.impl;

import com.hellobike.haxing.modules.wms.constans.WmsConstans;
import com.hellobike.haxing.modules.wms.dto.StockChangeDto;
import com.hellobike.haxing.modules.wms.entity.StockBatchLoc;
import com.hellobike.haxing.modules.wms.entity.StockPick;
import com.hellobike.haxing.modules.wms.entity.StockSync;
import com.hellobike.haxing.modules.wms.enums.StockSyncTypeEnum;
import com.hellobike.haxing.modules.wms.exception.WmsServiceException;
import com.hellobike.haxing.modules.wms.facade.WmsStockFacade;
import com.hellobike.haxing.modules.wms.service.IStockBatchLocService;
import com.hellobike.haxing.modules.wms.service.IStockPickService;
import com.hellobike.haxing.modules.wms.service.IStockSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Component("wmsStockFacade")
public class WmsStockFacadeImpl implements WmsStockFacade {

    @Autowired
    private IStockBatchLocService stockBatchLocService;

    @Autowired
    private IStockPickService stockPickService;

    @Autowired
    private IStockSyncService stockSyncService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public StockChangeDto addMainStock(StockChangeDto stockChangeDto) throws WmsServiceException {
        Long skuId = stockChangeDto.getSkuId();
        Long locId = stockChangeDto.getToLocId();
        Long batchId = stockChangeDto.getToBatchId();
        Long warehouseId = stockChangeDto.getWarehouseId();
        StockBatchLoc stockBatchLoc = stockBatchLocService.getBySkuIdAndLocIdAndBatchIdAndWarehouseId(skuId,locId,batchId,warehouseId);
        BigDecimal qty = stockChangeDto.getQty();
        if(null == stockBatchLoc){
            stockBatchLoc = new StockBatchLoc();
            stockBatchLoc.setSkuId(skuId);
            stockBatchLoc.setLocId(locId);
            stockBatchLoc.setBatchId(batchId);
            stockBatchLoc.setWarehouseId(warehouseId);
            stockBatchLoc.setAvailableQty(qty);
            stockBatchLoc.setHoldQty(BigDecimal.ZERO);
            if (!stockBatchLocService.save(stockBatchLoc)) {
                throw new WmsServiceException("新增主库存异常");
            }
        }else {
            if(!stockBatchLocService.addStock(stockBatchLoc.getId(), qty)){
                throw new WmsServiceException("更新主库存异常");
            }
        }
        stockChangeDto.setToStockId(stockBatchLoc.getId());
        //记录同步
        StockSync stockSync = new StockSync();
        stockSync.setSkuId(skuId);
        stockSync.setSyncType(StockSyncTypeEnum.ADD.getValue());
        stockSync.setSyncFlag(0);
        stockSync.setSyncQty(qty);
        stockSync.setWarehouseId(warehouseId);
        stockSyncService.save(stockSync);
        return stockChangeDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public StockChangeDto subMainStock(StockChangeDto stockChangeDto) throws WmsServiceException {
        StockBatchLoc stockBatchLoc = stockBatchLocService.getById(stockChangeDto.getStockId());
        if (stockBatchLoc == null) {
            throw new WmsServiceException("库存不存在");
        }
        if(stockBatchLoc.getAvailableQty().compareTo(stockChangeDto.getQty())<0){
            throw new WmsServiceException("库存不足");
        }
        if(!stockBatchLocService.subStock(stockBatchLoc.getId(),stockChangeDto.getQty())){
            throw new WmsServiceException("扣减主库存异常");
        }
        stockChangeDto.setFmStockId(stockBatchLoc.getId());
        return stockChangeDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public StockChangeDto addPickStock(StockChangeDto stockChangeDto) throws WmsServiceException {
        StockBatchLoc stockBatchLoc = stockBatchLocService.getById(stockChangeDto.getFmStockId());
        if (stockBatchLoc == null) {
            throw new WmsServiceException("库存不存在");
        }
        StockPick pick = stockPickService.getByStkBatchLocId(stockChangeDto.getFmStockId());
        if (pick == null) {
            pick = new StockPick();
            pick.setStkBatchLocId(stockBatchLoc.getId());
            pick.setBatchId(stockBatchLoc.getBatchId());
            pick.setLocId(WmsConstans.DEF_PICK_LOC_ID);
            pick.setPickQty(stockChangeDto.getQty());
            pick.setSkuId(stockBatchLoc.getSkuId());
            pick.setWarehouseId(stockBatchLoc.getWarehouseId());
            if (!stockPickService.save(pick)) {
                throw new WmsServiceException("新增拣货库存异常");
            }
        }else {
            if(!stockPickService.addStock(pick.getId(),stockChangeDto.getQty())){
                throw new WmsServiceException("增加拣货库存异常");
            }
        }
        stockChangeDto.setToStockId(pick.getId());
        return stockChangeDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public StockChangeDto subPickStock(StockChangeDto stockChangeDto) throws WmsServiceException {
        StockPick stockPick = stockPickService.getById(stockChangeDto.getStockId());
        if (stockPick == null) {
            throw new WmsServiceException("库存不存在");
        }
        if(stockPick.getPickQty().compareTo(stockChangeDto.getQty())<0){
            throw new WmsServiceException("库存不足");
        }
        if(!stockPickService.subStock(stockPick.getId(),stockChangeDto.getQty())){
            throw new WmsServiceException("扣减拣货库存异常");
        }
        stockChangeDto.setFmStockId(stockPick.getId());
        return stockChangeDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void increaseAndDecrease(Long id, Integer type, BigDecimal qty, String note, Long warehouseId, Integer userId) throws WmsServiceException {
        StockBatchLoc stockBatchLoc = stockBatchLocService.getById(id);
        if (stockBatchLoc == null) {
            throw new WmsServiceException("库存不存在");
        }
        if(qty.compareTo(BigDecimal.ZERO)<=0){
            throw new WmsServiceException("数量不能小于0");
        }
        if(type == 1){
            if(stockBatchLoc.getAvailableQty().compareTo(qty)<0){
                throw new WmsServiceException("库存不足");
            }
            if(!stockBatchLocService.subStock(stockBatchLoc.getId(),qty)){
                throw new WmsServiceException("扣减主库存异常");
            }
            //记录同步
            StockSync stockSync = new StockSync();
            stockSync.setSkuId(stockBatchLoc.getSkuId());
            stockSync.setSyncType(StockSyncTypeEnum.SUB.getValue());
            stockSync.setSyncFlag(0);
            stockSync.setSyncQty(qty);
            stockSync.setWarehouseId(warehouseId);
            stockSyncService.save(stockSync);
        }else {
            if(!stockBatchLocService.addStock(stockBatchLoc.getId(),qty)){
                throw new WmsServiceException("更新主库存异常");
            }
            //记录同步
            StockSync stockSync = new StockSync();
            stockSync.setSkuId(stockBatchLoc.getSkuId());
            stockSync.setSyncType(StockSyncTypeEnum.ADD.getValue());
            stockSync.setSyncFlag(0);
            stockSync.setSyncQty(qty);
            stockSync.setWarehouseId(warehouseId);
            stockSyncService.save(stockSync);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void back2MainStock(Long pickStockId, Long warehouseId, Integer userId) throws WmsServiceException {
        StockPick stockPick = stockPickService.getById(pickStockId);
        if (stockPick == null) {
            throw new WmsServiceException("库存不存在");
        }
        if(stockPick.getPickQty().compareTo(BigDecimal.ZERO)==0){
            throw new WmsServiceException("无需归还主库存");
        }
        if(!stockBatchLocService.addStock(stockPick.getStkBatchLocId(),stockPick.getPickQty())){
            throw new WmsServiceException("更新主库存异常");
        }
        if (!stockPickService.subStock(stockPick.getId(),stockPick.getPickQty())) {
            throw new WmsServiceException("更新拣货库存异常");
        }
        //记录同步
        StockSync stockSync = new StockSync();
        stockSync.setSkuId(stockPick.getSkuId());
        stockSync.setSyncType(StockSyncTypeEnum.ADD.getValue());
        stockSync.setSyncFlag(0);
        stockSync.setSyncQty(stockPick.getPickQty());
        stockSync.setWarehouseId(warehouseId);
        stockSyncService.save(stockSync);
    }
}
